﻿using Dao;
using MB.RuleBase.Common;
using Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class BOrder
    {
        /// <summary>
        /// 根据订单编号获取订单拣货任务主表信息
        /// </summary>
        /// <param name="OrderNO"></param>
        /// <returns></returns>
        public List<OrderInfo> getPindentInfo(string OrderNO)
        {
            List<OrderInfo> lst = new List<OrderInfo>();
            lst = OrderDao.NewInstance.GetOrderInfo(OrderNO);
            return lst;
        }

        /// <summary>
        /// 根据商品编号获取分拣灯无法拍灭导致的订单未分拣信息
        /// </summary>
        /// <param name="GoodeCode"></param>
        /// <param name="FLAG">00-未亮灯，01-亮灯未拍，00-亮灯拍灭</param>
        /// <returns></returns>
        public List<UnDealedOrderInfo> GetUndealedOrderInfo(DateTime RationDate, string BatchNO, string GoodeCode, string FLAG = "01")
        {
            List<UnDealedOrderInfo> lst = new List<UnDealedOrderInfo>();
            lst = OrderDao.NewInstance.GetUndealedOrderInfo(RationDate, BatchNO,GoodeCode, FLAG);
            return lst;
        }

        public bool UpdateUndealedOrderInfo(List<UnDealedOrderInfo> lst)
        {
            var _result = OrderDao.NewInstance.PostUndealedOrderInfo(lst);
            return _result;
        }

        public List<SaleOrderInfo> Getsaleorderinfo(string orderNo)
        {
            try
            {
                List<SaleOrderInfo> list = new List<SaleOrderInfo>();
                list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<SaleOrderInfo>("Order", "GetLacksaleOrder", orderNo);
                return list;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public bool SetLacksaleorderinfo( out string message,List<Saleorderlackinfo> LackInfo,int UserID)
        {
            message = null;
            bool outPut = false;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                List<Saleorderlackinfo> LackSaleorderList = new List<Saleorderlackinfo>();
                foreach (var item in LackInfo)
                {
                    Saleorderlackinfo _Saleorderlackinfo = new Saleorderlackinfo();
                    _Saleorderlackinfo.ORDERNO = item.ORDERNO;
                    _Saleorderlackinfo.GoodsCode = item.GoodsCode;
                    _Saleorderlackinfo.QTY = item.QTY;
                    _Saleorderlackinfo.LACKQTY = item.LACKQTY;
                    _Saleorderlackinfo.OperationUser = UserID;
                    LackSaleorderList.Add(_Saleorderlackinfo);
                }
                MB.Util.TraceEx.Write("插入销售订单缺货明细记录数量：" + LackSaleorderList.Count.ToString());
                outPut = InsertLackSaleorder(out message, tran, LackSaleorderList);
                if (outPut)
                {
                    tran.Commit();
                }
                else
                {
                    tran.Rollback();
                }
                return outPut;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                message = "记录失败！\n" + ex.Message;
                return outPut;
            }
            finally 
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }

        public bool InsertLackSaleorder(out string msg,DbTransaction transaction, List<Saleorderlackinfo> sertLackSaleorder)
        {
            msg = null;
            try 
	        {	        
		      using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("Order", "Setsaleorderlackinfo", sertLackSaleorder);
                }
                return true;
	        }
	        catch (Exception ex)
	        {
		        msg=ex.Message;
                return false;
	        }
          
        }
    }
}
